﻿#include "VAOneXMLAdaptorAcousticsNCTUserDefined.h"

#include "FITKAcousticsPhysics/FITKAcousticsNCTUserDefined.h"
#include "rapidxml.hpp"

namespace IO
{

    bool VAOneXMLAdaptorAcousticsNCTUserDefined::adaptR()
    {
        return readNCTUserDefined();
    }

    bool VAOneXMLAdaptorAcousticsNCTUserDefined::adaptW()
    {
        return writeNCTUserDefined();
    }

    bool VAOneXMLAdaptorAcousticsNCTUserDefined::readNCTUserDefined()
    {
        if (!_node)// 判断Fluid节点是否存在
        {
            return false;
        }

        bool b = true;
        //转换类型为
        Acoustics::FITKAcousticsNCTUserDefined* data = dynamic_cast<Acoustics::FITKAcousticsNCTUserDefined*>(_dataObj);

        if(!data)
        {
            return false;
        }

        //获取名称节点
        auto nameNode = _node->first_node("Name");

        if (nameNode == nullptr)
        {
            //材料数据中 名称为必要项 不可为空 并抛出异常日志
            consoleMessage(2, "NCTUserDefined name is null");
            return false;
        }

        QString strName = readStrNodeValue(nameNode, &b);

        if(b)
        {
            data->setDataObjectName(strName);//名称可以为空，但是节点不能为空
        }

        //获取颜色节点
        auto colorNode = _node->first_node("Color");
        float colorR = 0; float colorG = 0; float colorB = 0;
        //根据节点获取颜色
        readColorNodeValue(colorNode, colorR, colorG, colorB);
        //设置颜色
        data->setColor(colorR, colorG, colorB);

        ///获取频谱信息
        QString insertion_loss_resonant = readStrAttribute(_node, "insertion_loss_resonant", &b);

        if(b)
        {
            int id = getSpectraObjectIdByName(insertion_loss_resonant);
            data->setInsertionLossResonantID(id);
        }

        QString insertion_loss_nonresonant = readStrAttribute(_node, "insertion_loss_nonresonant", &b);

        if(b)
        {
            int id = getSpectraObjectIdByName(insertion_loss_nonresonant);
            data->setInsertionLossNonResonantID(id);
        }

        QString absorption_coeff = readStrAttribute(_node, "absorption_coeff", &b);

        if(b)
        {
            int id = getSpectraObjectIdByName(absorption_coeff);
            data->setAbsorptionCoeffID(id);
        }

        QString damping_flexure = readStrAttribute(_node, "damping_flexure", &b);

        if(b)
        {
            int id = getSpectraObjectIdByName(damping_flexure);
            data->setDampingFlexureID(id);
        }

        QString damping_shear = readStrAttribute(_node, "damping_shear", &b);

        if(b)
        {
            int id = getSpectraObjectIdByName(damping_shear);
            data->setDampingShearID(id);
        }

        QString damping_extension = readStrAttribute(_node, "damping_extension", &b);

        if(b)
        {
            int id = getSpectraObjectIdByName(damping_extension);
            data->setDampingExtensionID(id);
        }

        double mass_per_unit_area = readDoubleAttribute(_node, "mass_per_unit_area", &b);

        if(b)
        {
            data->setMassPerUnitArea(mass_per_unit_area);
        }

        return true;
    }

    bool VAOneXMLAdaptorAcousticsNCTUserDefined::writeNCTUserDefined()
    {
        if (!_node)// 判断节点是否存在
        {
            return false;
        }

        //转换类型为
        Acoustics::FITKAcousticsNCTUserDefined* data = dynamic_cast<Acoustics::FITKAcousticsNCTUserDefined*>(_dataObj);

        if(!data)
        {
            return false;
        }

        // 创建名称节点
        createNodeElement(_node, "Name", data->getDataObjectName());
        // 创建备注节点
        createNodeElement(_node, "Note", "");

        //创建颜色节点
        auto colorNode = createNodeElement(_node, "Color");

        //写入颜色节点信息
        if(colorNode)
        {
            float rgb[3] = {0.1f, 0.3f, 0.4f};
            data->getColor(rgb);
            writeAttribute(colorNode, "colorR", rgb[0]);
            writeAttribute(colorNode, "colorG", rgb[1]);
            writeAttribute(colorNode, "colorB", rgb[2]);
        }

        writeAttribute(_node, "mass_per_unit_area", data->getMassPerUnitArea());

        //其他频率

        QString insertion_loss_resonant = getSpectraObjectNameByID(data->getInsertionLossResonantID());

        if(!insertion_loss_resonant.isEmpty())
        {
            writeAttribute(_node, "insertion_loss_resonant", insertion_loss_resonant);
        }

        QString insertion_loss_nonresonant = getSpectraObjectNameByID(data->getInsertionLossNonResonantID());

        if(!insertion_loss_nonresonant.isEmpty())
        {
            writeAttribute(_node, "insertion_loss_nonresonant", insertion_loss_nonresonant);
        }

        QString absorption_coeff = getSpectraObjectNameByID(data->getAbsorptionCoeffID());

        if(!absorption_coeff.isEmpty())
        {
            writeAttribute(_node, "absorption_coeff", absorption_coeff);
        }

        QString damping_flexure = getSpectraObjectNameByID(data->getDampingFlexureID());

        if(!damping_flexure.isEmpty())
        {
            writeAttribute(_node, "damping_flexure", damping_flexure);
        }

        QString damping_shear = getSpectraObjectNameByID(data->getDampingShearID());

        if(!damping_shear.isEmpty())
        {
            writeAttribute(_node, "damping_shear", damping_shear);
        }

        QString damping_extension = getSpectraObjectNameByID(data->getDampingExtensionID());

        if(!damping_extension.isEmpty())
        {
            writeAttribute(_node, "damping_extension", damping_extension);
        }

        return true;
    }

}
